using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library;
using System.Collections.Specialized;
using System.Collections;
using System.Net;
using System.IO;
using System.Web;

namespace AutoGear.BLL
{
    public class SiteUtility : Security
    {
        public static string TempFolder
        {
            get { return "~/upload/temp/"; }
        }

        public static int SuperRootCategoryId
        {
            get { return 1; }
        }

        public static int GridDefaultPageSize
        {
            get { return 20; }
        }


        public static int MaximumCartItemQuantity
        {
            get { return 9999; }
        }

        public static int MaximumQuantityLength
        {
            get { return 4; }
        }

        public static string CleanBrandName(string rawBrandName)
        {
            StringBuilder cleanName = new StringBuilder(rawBrandName.Trim());

            for (int i = 0; i < cleanName.Length; i++)
            {
                if (!char.IsLetterOrDigit(cleanName[i]) && cleanName[i] != '-')
                {
                    cleanName[i] = '-';
                }
            }
            return cleanName.Replace("---", "-").Replace("--", "-").ToString();
        }

        public static string CleanCategoryName(string rawCategoryName)
        {
            /*StringBuilder cleanName = new StringBuilder(rawCategoryName.Trim());

            for (int i = 0; i < cleanName.Length; i++)
            {
                if (!char.IsLetterOrDigit(cleanName[i]) && cleanName[i] != '-')
                {
                    cleanName[i] = '-';
                }
            }
            return cleanName.Replace("---", "-").Replace("--", "-").ToString();*/
            return System.Text.RegularExpressions.Regex.Replace(rawCategoryName.ToLower(), @"[^\w\s\-]", "").Replace(" ", "-");
        }

        public static FilterType StringToFilterType(string filterTypeName)
        {
            if (string.IsNullOrEmpty(filterTypeName))
                return FilterType.All;

            if (filterTypeName.ToUpper() == "A")
                return FilterType.A;
            else if (filterTypeName.ToUpper() == "B")
                return FilterType.B;
            else if (filterTypeName.ToUpper() == "C")
                return FilterType.C;
            else if (filterTypeName.ToUpper() == "D")
                return FilterType.D;
            else if (filterTypeName.ToUpper() == "E")
                return FilterType.E;
            else if (filterTypeName.ToUpper() == "F")
                return FilterType.F;
            else if (filterTypeName.ToUpper() == "G")
                return FilterType.G;
            else if (filterTypeName.ToUpper() == "H")
                return FilterType.H;
            else if (filterTypeName.ToUpper() == "I")
                return FilterType.I;
            else if (filterTypeName.ToUpper() == "J")
                return FilterType.J;
            else if (filterTypeName.ToUpper() == "K")
                return FilterType.K;
            else if (filterTypeName.ToUpper() == "L")
                return FilterType.L;
            else if (filterTypeName.ToUpper() == "M")
                return FilterType.M;
            else if (filterTypeName.ToUpper() == "N")
                return FilterType.N;
            else if (filterTypeName.ToUpper() == "O")
                return FilterType.O;
            else if (filterTypeName.ToUpper() == "P")
                return FilterType.P;
            else if (filterTypeName.ToUpper() == "Q")
                return FilterType.Q;
            else if (filterTypeName.ToUpper() == "R")
                return FilterType.R;
            else if (filterTypeName.ToUpper() == "S")
                return FilterType.S;
            else if (filterTypeName.ToUpper() == "T")
                return FilterType.T;
            else if (filterTypeName.ToUpper() == "U")
                return FilterType.U;
            else if (filterTypeName.ToUpper() == "V")
                return FilterType.V;
            else if (filterTypeName.ToUpper() == "W")
                return FilterType.W;
            else if (filterTypeName.ToUpper() == "X")
                return FilterType.X;
            else if (filterTypeName.ToUpper() == "Y")
                return FilterType.Y;
            else if (filterTypeName.ToUpper() == "Z")
                return FilterType.Z;
            else if (filterTypeName.ToUpper() == "ALL")
                return FilterType.All;
            else if (filterTypeName.ToUpper() == "ZEROTONINE")
                return FilterType.ZeroToNine;
            else
                return FilterType.All;
        }

        internal static string CleanProductName(string rawProductName)
        {
            rawProductName = System.Web.HttpUtility.HtmlDecode(rawProductName.Trim());
            string temp = System.Text.RegularExpressions.Regex.Replace(rawProductName.ToLower(), @"[^\w\s\-]", "").Replace(" ", "-");

            /*for (int i = 0; i < rawProductName.Length; i++)
            {
                if (char.IsLetterOrDigit(rawProductName[i]))
                {
                    temp = temp + rawProductName[i];
                }
                else if (i + 1 < rawProductName.Length && char.IsLetterOrDigit(rawProductName[i + 1]))
                {
                    temp = temp + "-";
                }
            }*/

            return temp;
        }

        internal static string CleanCopounName(string rawCopounName)
        {
            rawCopounName = System.Web.HttpUtility.HtmlDecode(rawCopounName.Trim());
            string temp = "";

            for (int i = 0; i < rawCopounName.Length; i++)
            {
                if (char.IsLetterOrDigit(rawCopounName[i]))
                {
                    temp = temp + rawCopounName[i];
                }
                else if (i + 1 < rawCopounName.Length && char.IsLetterOrDigit(rawCopounName[i + 1]))
                {
                    temp = temp + "-";
                }
            }

            return temp;

        }

        public static ProductShippingMethod ResolveShippingMethod(int shippingMethod)
        {
            if (shippingMethod == (int)ProductShippingMethod.Express)
                return ProductShippingMethod.Express;
            else if (shippingMethod == (int)ProductShippingMethod.Overnight)
                return ProductShippingMethod.Overnight;
            else if (shippingMethod == (int)ProductShippingMethod.Standard)
                return ProductShippingMethod.Standard;
            else
                return ProductShippingMethod.Standard;
        }

        public static string HideCharacter(string originalText, char replacement, int howManyChar, bool startFromLeft)
        {
            string result = originalText;

            if (originalText.Length < howManyChar)
                howManyChar = originalText.Length;
            if (startFromLeft)
            {
                //result = originalText.Substring(originalText.Length - howManyChar, originalText.Length - howManyChar);
                result = originalText.Substring(howManyChar, originalText.Length - howManyChar);
                result = new string(replacement, howManyChar) + result;
                return result;
            }
            else
            {
                result = originalText.Substring(0, originalText.Length - howManyChar);
                result = new string(replacement, howManyChar) + result;
                return result;
            }
        }

        public static string GetStateCodeFromStateName(string stateName)
        {
            if (stateName == "Alabama") return "AL";
            if (stateName == "Alaska") return "AK";
            if (stateName == "Alberta") return "AB";
            if (stateName == "Arizona") return "AZ";
            if (stateName == "Arkansas") return "AR";
            if (stateName == "British Columbia") return "BC";
            if (stateName == "California") return "CA";
            if (stateName == "Colorado") return "CO";
            if (stateName == "Connecticut") return "CT";
            if (stateName == "Delaware") return "DE";
            if (stateName == "District of Columbia") return "DC";
            if (stateName == "Florida") return "FL";
            if (stateName == "Georgia") return "GA";
            if (stateName == "Hawaii") return "HI";
            if (stateName == "Idaho") return "ID";
            if (stateName == "Illinois") return "IL";
            if (stateName == "Indiana") return "IN";
            if (stateName == "Iowa") return "IA";
            if (stateName == "Kansas") return "KS";
            if (stateName == "Kentucky") return "KY";
            if (stateName == "Louisiana") return "LA";
            if (stateName == "Maine") return "ME";
            if (stateName == "Manitoba") return "MB";
            if (stateName == "Maryland") return "MD";
            if (stateName == "Massachusetts") return "MA";
            if (stateName == "Michigan") return "MI";
            if (stateName == "Minnesota") return "MN";
            if (stateName == "Mississippi") return "MS";
            if (stateName == "Missouri") return "MO";
            if (stateName == "Montana") return "MT";
            if (stateName == "Nebraska") return "NE";
            if (stateName == "Nevada") return "NV";
            if (stateName == "New Brunswick") return "NB";
            if (stateName == "New Hampshire") return "NH";
            if (stateName == "New Jersey") return "NJ";
            if (stateName == "New Mexico") return "NM";
            if (stateName == "New York") return "NY";
            if (stateName == "Newfoundland and Labrador") return "NF";
            if (stateName == "North Carolina") return "NC";
            if (stateName == "North Dakota") return "ND";
            if (stateName == "Northwest Territories") return "NT";
            if (stateName == "Nova Scotia") return "NS";
            if (stateName == "Ohio") return "OH";
            if (stateName == "Oklahoma") return "OK";
            if (stateName == "Ontario") return "ON";
            if (stateName == "Oregon") return "OR";
            if (stateName == "Pennsylvania") return "PA";
            if (stateName == "Prince Edward Island") return "PE";
            if (stateName == "Puerto Rico") return "PR";
            if (stateName == "Quebec") return "QC";
            if (stateName == "Rhode Island") return "RI";
            if (stateName == "Saskatchewan") return "SK";
            if (stateName == "South Carolina") return "SC";
            if (stateName == "South Dakota") return "SD";
            if (stateName == "Tennessee") return "TN";
            if (stateName == "Texas") return "TX";
            if (stateName == "Utah") return "UT";
            if (stateName == "Vermont") return "VT";
            if (stateName == "Virginia") return "VA";
            if (stateName == "Washington") return "WA";
            if (stateName == "West Virginia") return "WV";
            if (stateName == "Wisconsin") return "WI";
            if (stateName == "Wyoming") return "WY";
            if (stateName == "Yukon") return "YT";

            return "NA";
        }



        public static string GetCountryCodeFromCountryName(string countryName)
        {
            if (countryName == "United States") return "US";
            if (countryName == "Canada") return "CA";
            if (countryName == "Afghanistan") return "AF";
            if (countryName == "Albania") return "AL";
            if (countryName == "Algeria") return "DZ";
            if (countryName == "American Samoa") return "AS";
            if (countryName == "Andorra") return "AD";
            if (countryName == "Angola") return "AO";
            if (countryName == "Anguilla") return "AI";
            if (countryName == "Antarctica") return "AQ";
            if (countryName == "Antigua and Barbuda") return "AG";
            if (countryName == "Argentina") return "AR";
            if (countryName == "Armenia") return "AM";
            if (countryName == "Aruba") return "AW";
            if (countryName == "Australia") return "AU";
            if (countryName == "Austria") return "AT";
            if (countryName == "Azerbaijan") return "AZ";
            if (countryName == "Bahamas") return "BS";
            if (countryName == "Bahrain") return "BH";
            if (countryName == "Bangladesh") return "BD";
            if (countryName == "Barbados") return "BB";
            if (countryName == "Belarus") return "BY";
            if (countryName == "Belgium") return "BE";
            if (countryName == "Belize") return "BZ";
            if (countryName == "Benin") return "BJ";
            if (countryName == "Bermuda") return "BM";
            if (countryName == "Bhutan") return "BT";
            if (countryName == "Bolivia") return "BO";
            if (countryName == "Bosnia and Herzegovina") return "BA";
            if (countryName == "Botswana") return "BW";
            if (countryName == "Bouvet Island") return "BV";
            if (countryName == "Brazil") return "BR";
            if (countryName == "British Indian Ocean Territory") return "IO";
            if (countryName == "Brunei Darussalam") return "BN";
            if (countryName == "Bulgaria") return "BG";
            if (countryName == "Burkina Faso") return "BF";
            if (countryName == "Burundi") return "BI";
            if (countryName == "Cambodia") return "KH";
            if (countryName == "Cameroon") return "CM";
            if (countryName == "Cape Verde") return "CV";
            if (countryName == "Cayman Islands") return "KY";
            if (countryName == "Central African Republic") return "CF";
            if (countryName == "Chad") return "TD";
            if (countryName == "Chile") return "CL";
            if (countryName == "China") return "CN";
            if (countryName == "Christmas Island") return "CX";
            if (countryName == "Cocos (Keeling) Islands") return "CC";
            if (countryName == "Colombia") return "CO";
            if (countryName == "Comoros") return "KM";
            if (countryName == "Congo") return "CG";
            if (countryName == "Congo Democratic Republic") return "CD";
            if (countryName == "Cook Islands") return "CK";
            if (countryName == "Costa Rica") return "CR";
            if (countryName == "Croatia") return "HR";
            if (countryName == "Cuba") return "CU";
            if (countryName == "Cyprus") return "CY";
            if (countryName == "Czech Republic") return "CZ";
            if (countryName == "Czechoslovakia (former)") return "CS";
            if (countryName == "Denmark") return "DK";
            if (countryName == "Djibouti") return "DJ";
            if (countryName == "Dominica") return "DM";
            if (countryName == "Dominican Republic") return "DO";
            if (countryName == "East Timor") return "TP";
            if (countryName == "Ecuador") return "EC";
            if (countryName == "Egypt") return "EG";
            if (countryName == "El Salvador") return "SV";
            if (countryName == "Equatorial Guinea") return "GQ";
            if (countryName == "Eritrea") return "ER";
            if (countryName == "Estonia") return "EE";
            if (countryName == "Ethiopia") return "ET";
            if (countryName == "F.Y.R.O.M. (Macedonia)") return "MK";
            if (countryName == "Falkland Islands (Malvinas)") return "FK";
            if (countryName == "Faroe Islands") return "FO";
            if (countryName == "Fiji") return "FJ";
            if (countryName == "Finland") return "FI";
            if (countryName == "France") return "FR";
            if (countryName == "France Metropolitan") return "FX";
            if (countryName == "French Guiana") return "GF";
            if (countryName == "French Polynesia") return "PF";
            if (countryName == "French Southern Territories") return "TF";
            if (countryName == "Gabon") return "GA";
            if (countryName == "Gambia") return "GM";
            if (countryName == "Georgia") return "GE";
            if (countryName == "Germany") return "DE";
            if (countryName == "Ghana") return "GH";
            if (countryName == "Gibraltar") return "GI";
            if (countryName == "United Kingdom (Great Britain)") return "GB";
            if (countryName == "Greece") return "GR";
            if (countryName == "Greenland") return "GL";
            if (countryName == "Grenada") return "GD";
            if (countryName == "Guadeloupe") return "GP";
            if (countryName == "Guam") return "GU";
            if (countryName == "Guatemala") return "GT";
            if (countryName == "Guinea") return "GN";
            if (countryName == "Guinea") return "GW";
            if (countryName == "Guyana") return "GY";
            if (countryName == "Haiti") return "HT";
            if (countryName == "Heard and McDonald Islands") return "HM";
            if (countryName == "Honduras") return "HN";
            if (countryName == "Hong Kong") return "HK";
            if (countryName == "Hungary") return "HU";
            if (countryName == "Iceland") return "IS";
            if (countryName == "India") return "IN";
            if (countryName == "Indonesia") return "ID";
            if (countryName == "Iran") return "IR";
            if (countryName == "Iraq") return "IQ";
            if (countryName == "Ireland") return "IE";
            if (countryName == "Israel") return "IL";
            if (countryName == "Italy") return "IT";
            if (countryName == "Jamaica") return "JM";
            if (countryName == "Japan") return "JP";
            if (countryName == "Jordan") return "JO";
            if (countryName == "Kazakhstan") return "KZ";
            if (countryName == "Kenya") return "KE";
            if (countryName == "Kiribati") return "KI";
            if (countryName == "North Korea") return "KP";
            if (countryName == "South Korea") return "KR";
            if (countryName == "Kuwait") return "KW";
            if (countryName == "Kyrgyzstan") return "KG";
            if (countryName == "Laos") return "LA";
            if (countryName == "Latvia") return "LV";
            if (countryName == "Lebanon") return "LB";
            if (countryName == "Lesotho") return "LS";
            if (countryName == "Liberia") return "LR";
            if (countryName == "Libya") return "LY";
            if (countryName == "Liechtenstein") return "LI";
            if (countryName == "Lithuania") return "LT";
            if (countryName == "Luxembourg") return "LU";
            if (countryName == "Macau") return "MO";
            if (countryName == "Madagascar") return "MG";
            if (countryName == "Malawi") return "MW";
            if (countryName == "Malaysia") return "MY";
            if (countryName == "Maldives") return "MV";
            if (countryName == "Mali") return "ML";
            if (countryName == "Malta") return "MT";
            if (countryName == "Marshall Islands") return "MH";
            if (countryName == "Martinique") return "MQ";
            if (countryName == "Mauritania") return "MR";
            if (countryName == "Mauritius") return "MU";
            if (countryName == "Mayotte") return "YT";
            if (countryName == "Mexico") return "MX";
            if (countryName == "Micronesia") return "FM";
            if (countryName == "Moldova") return "MD";
            if (countryName == "Monaco") return "MC";
            if (countryName == "Mongolia") return "MN";
            if (countryName == "Montserrat") return "MS";
            if (countryName == "Morocco") return "MA";
            if (countryName == "Mozambique") return "MZ";
            if (countryName == "Myanmar") return "MM";
            if (countryName == "Namibia") return "NA";
            if (countryName == "Nauru") return "NR";
            if (countryName == "Nepal") return "NP";
            if (countryName == "Netherlands") return "NL";
            if (countryName == "Netherlands Antilles") return "AN";
            if (countryName == "Neutral Zone") return "NT";
            if (countryName == "New Caledonia") return "NC";
            if (countryName == "New Zealand") return "NZ";
            if (countryName == "Nicaragua") return "NI";
            if (countryName == "Niger") return "NE";
            if (countryName == "Nigeria") return "NG";
            if (countryName == "Niue") return "NU";
            if (countryName == "Norfolk Island") return "NF";
            if (countryName == "Northern Mariana Islands") return "MP";
            if (countryName == "Norway") return "NO";
            if (countryName == "Oman") return "OM";
            if (countryName == "Pakistan") return "PK";
            if (countryName == "Palau") return "PW";
            if (countryName == "Panama") return "PA";
            if (countryName == "Papua New Guinea") return "PG";
            if (countryName == "Paraguay") return "PY";
            if (countryName == "Peru") return "PE";
            if (countryName == "Philippines") return "PH";
            if (countryName == "Pitcairn") return "PN";
            if (countryName == "Poland") return "PL";
            if (countryName == "Portugal") return "PT";
            if (countryName == "Puerto Rico") return "PR";
            if (countryName == "Qatar") return "QA";
            if (countryName == "Reunion") return "RE";
            if (countryName == "Romania") return "RO";
            if (countryName == "Russian Federation") return "RU";
            if (countryName == "Rwanda") return "RW";
            if (countryName == "S. Georgia and S. Sandwich Isls.") return "GS";
            if (countryName == "Saint Kitts and Nevis") return "KN";
            if (countryName == "Saint Lucia") return "LC";
            if (countryName == "Saint Vincent &amp; the Grenadines") return "VC";
            if (countryName == "Samoa") return "WS";
            if (countryName == "San Marino") return "SM";
            if (countryName == "Sao Tome and Principe") return "ST";
            if (countryName == "Saudi Arabia") return "SA";
            if (countryName == "Senegal") return "SN";
            if (countryName == "Seychelles") return "SC";
            if (countryName == "Sierra Leone") return "SL";
            if (countryName == "Singapore") return "SG";
            if (countryName == "Slovak Republic") return "SK";
            if (countryName == "Slovenia") return "SI";
            if (countryName == "Solomon Islands") return "SB";
            if (countryName == "Somalia") return "SO";
            if (countryName == "South Africa") return "ZA";
            if (countryName == "Spain") return "ES";
            if (countryName == "Sri Lanka") return "LK";
            if (countryName == "St. Helena") return "SH";
            if (countryName == "St. Pierre and Miquelon") return "PM";
            if (countryName == "Sudan") return "SD";
            if (countryName == "Suriname") return "SR";
            if (countryName == "Svalbard &amp; Jan Mayen Islands") return "SJ";
            if (countryName == "Swaziland") return "SZ";
            if (countryName == "Sweden") return "SE";
            if (countryName == "Switzerland") return "CH";
            if (countryName == "Syria") return "SY";
            if (countryName == "Taiwan") return "TW";
            if (countryName == "Tajikistan") return "TJ";
            if (countryName == "Tanzania") return "TZ";
            if (countryName == "Thailand") return "TH";
            if (countryName == "Togo") return "TG";
            if (countryName == "Tokelau") return "TK";
            if (countryName == "Tonga") return "TO";
            if (countryName == "Trinidad and Tobago") return "TT";
            if (countryName == "Tunisia") return "TN";
            if (countryName == "Turkey") return "TR";
            if (countryName == "Turkmenistan") return "TM";
            if (countryName == "Turks and Caicos Islands") return "TC";
            if (countryName == "Tuvalu") return "TV";
            if (countryName == "Uganda") return "UG";
            if (countryName == "Ukraine") return "UA";
            if (countryName == "United Arab Emirates") return "AE";
            if (countryName == "Uruguay") return "UY";
            if (countryName == "US Minor Outlying Islands") return "UM";
            if (countryName == "USSR (former)") return "SU";
            if (countryName == "Uzbekistan") return "UZ";
            if (countryName == "Vanuatu") return "VU";
            if (countryName == "Vatican City State (Holy See)") return "VA";
            if (countryName == "Venezuela") return "VE";
            if (countryName == "Viet Nam") return "VN";
            if (countryName == "Virgin Islands (British)") return "VG";
            if (countryName == "Virgin Islands (U.S.)") return "VI";
            if (countryName == "Wallis and Futuna Islands") return "WF";
            if (countryName == "Western Sahara") return "EH";
            if (countryName == "Yemen") return "YE";
            if (countryName == "Yugoslavia") return "YU";
            if (countryName == "Zaire") return "ZR";
            if (countryName == "Zambia") return "ZM";
            if (countryName == "Zimbabwe") return "ZW";

            return "NA";
        }

        public static string ReadWebPage(string url)
        {
            string result;
            WebResponse response;
            WebRequest request;
            request = HttpWebRequest.Create(url);
            response = request.GetResponse();
            using (StreamReader sReader = new StreamReader(response.GetResponseStream()))
            {
                result = sReader.ReadToEnd();
                sReader.Close();
            }
            return result;
        }
        public static bool IsSecuredConnection
        {
            get
            {
                bool status = false;
                if (HttpContext.Current.Request.ServerVariables["HTTPS"] != null)
                    status = (HttpContext.Current.Request.ServerVariables["HTTPS"].ToString().ToLower() == "on");
                return status;
            }
        }
    }
}
